package mutiyi;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * Author:yang
 * Date:2024-03-06 14:47
 * A端发送heartbeat
 */
public class AClient {

    public static void main(String[] args) throws MqttException, InterruptedException {
        String broker = "tcp://81.71.46.14:1882";
        String clientId = "A";
        String topic = "testHeartbeat";
        String username = "test1";
        String password = "test1";

        MqttClient mqttClient = new MqttClient(broker, clientId);
        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setUserName(username);
        mqttConnectOptions.setPassword(password.toCharArray());
        mqttConnectOptions.setCleanSession(true);

        mqttClient.connect(mqttConnectOptions);

        while (true){
            String message = "消息A：hello" + System.currentTimeMillis();
            MqttMessage mqttMessage = new MqttMessage(message.getBytes());
            mqttClient.publish(topic, mqttMessage);

            Thread.sleep(1000);
        }
    }
}
